package linker;

import java.sql.ResultSet;
import java.sql.SQLException;

import dto.ExecStepDTO;
import dto.FechaDTO;

public class ExecPlan {
	
	private static ExecPlan instance = null;
	
	private static ExecPlan getInstance() throws SQLException {
		if (instance == null)
			instance = new ExecPlan();
		return instance;
	}
	
	public static boolean hasNext() throws SQLException {
		return (getInstance().table.getRow() < getInstance().size);
	}
	
	public static ExecStepDTO next() throws SQLException {
		getInstance().table.next();
		ExecStepDTO aux = new ExecStepDTO();
		aux.fecha = new FechaDTO(getInstance().table.getString("fecha"));
		aux.modulo = getInstance().table.getString("modulo");
		aux.servidor = getInstance().table.getString("servidor");
		return aux;
	}
	
	public static void mark() throws SQLException {
		getInstance().table.updateInt("procesado", 1);
		getInstance().table.updateRow();
	}
	
	private ResultSet table = null;
	private int size = 0;
	
	private ExecPlan() throws SQLException {
		String aux = "SELECT * from " + CONFIGSQL.getDatabaseName() + "..exec_plan where procesado = 0";
		table = LinkerSQL.runCommand(aux);
		table.last();
		size = table.getRow();
		table.beforeFirst();
	}
}
